using System.Collections;
using System.Collections.Generic;
using ComponentSystem;
using UnityEngine;
using UnityEngine.Events;
using YooAsset;
using YooAssetsSystem;

public class TestYooDemo : MonoBehaviour
{
    public GameObject obj;

    public void Do_Update()
    {
        ComponentFactory.Create<YAService_Entity>();

        Test_YooAssetPkgDownload_AddPkg("HotRes");
    }


    #region 临时的更新流程

    /// <summary>
    /// 下载 分包
    /// </summary>
    public void Test_YooAssetPkgDownload_AddPkg(string name)
    {
        EPlayMode playMode = EPlayMode.EditorSimulateMode;
        string url = "";
        string appVersion = "1";
        string packageName = name;
        string[] loadTags = new string[0];
        bool isCustomizeUrl = false;

        if (name.Equals("HotRes"))
        {
            bool isDefaultPackage = true;
            YAService_Entity.Instance.DownloadPackage(playMode, url, appVersion, packageName, isDefaultPackage, loadTags,
                    isCustomizeUrl,
                    Event_ChangeStateEvent, Event_PatchDoneEvent_DefaultPackage, Event_UpdateFailedEvent,
                    Event_NeedDownloadEvent)
                .Forget();
        }
        if (name.Equals("test2"))
        {
            bool isDefaultPackage = false;
            YAService_Entity.Instance.DownloadPackage(playMode, url, appVersion, packageName, isDefaultPackage, loadTags,
                    isCustomizeUrl,
                    Event_ChangeStateEvent, Event_PatchDoneEvent_AddPkg, Event_UpdateFailedEvent,
                    Event_NeedDownloadEvent)
                .Forget();
        }
    }

    /// <summary>
    /// 回调事件 下载 流程 状态切换
    /// </summary>
    /// <param name="msg"></param>
    private void Event_ChangeStateEvent(string msg)
    {
        Debug.Log($"{GetType().FullName}:: ChangeStateEvent: {msg}");
    }

    /// <summary>
    /// 回调事件 下载 结束 默认包的处理
    /// </summary>
    /// <param name="msg"></param>
    private void Event_PatchDoneEvent_DefaultPackage(string msg)
    {
        Debug.Log($"{GetType().FullName}:: PatchDoneEvent: {msg}");
        //Test_YooAssetPkgDownload_AddPkg("test2");

        obj.SetActive(true);
    }

    /// <summary>
    /// 回调事件 下载 结束 分包的处理
    /// </summary>
    /// <param name="msg"></param>
    private void Event_PatchDoneEvent_AddPkg(string msg)
    {
        Debug.Log($"{GetType().FullName}:: PatchDoneEvent: {msg}");
        obj.SetActive(true);
    }

    /// <summary>
    /// 回调事件 下载 失败
    /// </summary>
    /// <param name="msg"></param>
    private void Event_UpdateFailedEvent(string msg)
    {
        Debug.Log($"{GetType().FullName}:: UpdateFailedEvent: {msg}");
    }

    /// <summary>
    /// 回调事件 下载 需要下载的情况
    /// 里面有个UnityAction，可以给UI来使用
    /// 例如： 给用户提示有多少内容需要下载，点击开始才进行下载
    /// </summary>
    /// <param name="fileNum">总文件数量</param>
    /// <param name="totalSize">总大小</param>
    /// <param name="toDownload">开始下载的事件</param>
    private void Event_NeedDownloadEvent(int fileNum, long totalSize, UnityAction toDownload)
    {
        Debug.Log($"{GetType().FullName}:: Event_NeedDownloadEvent 文件数: {fileNum} -- 总大小: {totalSize}");
        toDownload?.Invoke();
    }

    #endregion
}
